#ifndef WindowColorMap_First
#define WindowColorMap_First
#ifdef __GNUG__
#pragma interface
#endif

#include "RefCounted.h"
#include "Ink.h"

class Port;
class WindowPort;

#if 0
#   define MAXREDS      5
#   define MAXGREENS    9
#   define MAXBLUES     5
#else
#   define MAXREDS      6
#   define MAXGREENS    6
#   define MAXBLUES     6
#endif
#   define MAXGREYS     15

#define Pixel(r,g,b) \
	((((r)*(MAXREDS-1))/255) * MAXGREENS * MAXBLUES \
	+ (((g)*(MAXGREENS-1))/255) * MAXBLUES \
	+ (((b)*(MAXBLUES-1))/255) \
	)

//---- WindowColorMap ----------------------------------------------------------

class WindowColorMap : public RefCounted {
public:
	MetaDef(WindowColorMap);
	WindowColorMap(bool readwrite);
	WindowColorMap(WindowColorMap *from);

	int GetId()
		{ return id; }
	u_long RGB2Index(RGB *rgb, RGB *result= 0)
		{ return DevRGB2Index(rgb, result); }
	void Index2RGB(u_long ix, RGB *result)
		{ DevIndex2RGB(ix, result); }
	u_long AllocateAndSetCell(long id, RGB *rgb, WindowPort*);
	void Install(Port *port)
		{ if (changed > 0) DevInstall(port); }
	bool IsGrey()
		{ return grey; }
	WindowColorMap *MakeCopy(WindowColorMap *from)
		{ return DevMakeCopy(from); }

protected:
	~WindowColorMap();
	virtual u_long DevAllocateAndSetCell(long id, RGB *rgb, WindowPort*);
	virtual void DevInstall(Port *port);
	virtual u_long DevRGB2Index(RGB *rgb, RGB *result);
	virtual u_long DevRGB2Index2(RGB *rgb);
	virtual void DevIndex2RGB(u_long ix, RGB *result);
	virtual WindowColorMap *DevMakeCopy(WindowColorMap *from);

public:
	int changed;

protected:
	bool rw, grey;
	int id;

private:
	static int cnt;
};

//---- WindowIndexMapper -------------------------------------------------------

class WindowIndexMapper : public WindowColorMap {
public:
	MetaDef(WindowIndexMapper);
	WindowIndexMapper(bool readwrite= TRUE);
	WindowIndexMapper(WindowIndexMapper *from);
protected:
	~WindowIndexMapper();
	u_long DevRGB2Index(RGB *rgb, RGB *result);
	void DevIndex2RGB(u_long ix, RGB *result);
protected:
	int length;
	RGB *realmap;
	u_long *graymap, *rgbmap;
};

//---- ColorDirectMapper -------------------------------------------------------

class ColorDirectMapper : public WindowColorMap {
public:
	MetaDef(ColorDirectMapper);
	ColorDirectMapper(u_long rm, u_long gm, u_long bm, bool readwrite);
	u_long DevRGB2Index(RGB *c, RGB *result);
	void DevIndex2RGB(u_long ix, RGB *result);
protected:
	u_long rmask, gmask, bmask;
	int rshift, gshift, bshift;
	int rmul, gmul, bmul;
};

#endif

